package cn.edu.xmu.javaee.productdemojpa.service;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.productdemojpa.dao.ProductDao;
import cn.edu.xmu.javaee.productdemojpa.dao.bo.Product;
import cn.edu.xmu.javaee.productdemojpa.service.vo.CustomerProductVo;
import cn.edu.xmu.javaee.productdemojpa.service.vo.ProductVo;
import cn.edu.xmu.javaee.productdemojpa.util.CloneFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class ProductService {

    private final ProductDao productDao;

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id 商品id
     * @return 商品对象
     */
    public CustomerProductVo retrieveProductByID(Long shopId, Long id) throws BusinessException {
        assert shopId != null && id != null;
        log.debug("findProductById: shopId = {}, id = {}",shopId, id);
        Product product =  this.productDao.findById(shopId, id);
        CustomerProductVo customerProductVo = CloneFactory.copy(new CustomerProductVo(), product);
        return customerProductVo;
    }


    public ProductVo retrieveSimpleProductByID(Long shopId, Long id) throws BusinessException {
        assert shopId != null && id != null;
        log.debug("retrieveSimpleProductByID: shopId = {}, id = {}",shopId, id);
        Product product = this.productDao.findSimpleProductById(shopId, id);
        log.debug("retrieveSimpleProductByID: product = {}",product);
        return CloneFactory.copy(new ProductVo(), product);
    }

    /**
     * 用商品名称搜索商品
     * @param shopId 商铺id
     * @param name 商品名称
     *
     * @return 商品对象
     */
    public List<CustomerProductVo> retrieveCustomerProductByName(Long shopId, String name) throws BusinessException{
        assert shopId != null && name != null;
        List<Product> productList =  this.productDao.retrieveByName(shopId, name);
        List<CustomerProductVo> data = productList.stream().map(o->CloneFactory.copy(new CustomerProductVo(),o)).collect(Collectors.toList());
        return data;
    }

    /**
     * 用商品名称搜索商品
     * @param shopId 商铺id
     * @param name 商品名称
     *
     * @return 商品对象
     */
    public List<ProductVo> retrieveProductByName(Long shopId, String name) throws BusinessException{
        assert shopId != null && name != null;
        List<Product> productList =  this.productDao.retrieveByName(shopId, name);
        List<ProductVo> data = productList.stream().map(o->CloneFactory.copy(new ProductVo(), o)).collect(Collectors.toList());
        return data;
    }

    /**
     * 新增商品
     * @param product 新商品信息
     * @return 新商品
     */
    public ProductVo createProduct(Product product) throws BusinessException{
        assert product != null;
        Product retProduct = this.productDao.insert(product);
        Product newProduct = this.productDao.findById(retProduct.getShopId(),retProduct.getId());
        ProductVo vo = CloneFactory.copy(new ProductVo(), newProduct);
        return vo;
    }


    /**
     * 修改商品
     * @param product 修改商品信息
     */
    public void modifyProduct(Product product) throws BusinessException{
        assert product != null && product.getId() != null;
        this.productDao.update(product);
    }

    /** 删除商品
     * @param id 商品id
     * @return 删除是否成功
     */
    public void deleteProduct(Long id) throws BusinessException {
        assert id != null;
        this.productDao.delete(id);
    }
}
